TranSMART 17.1 REST API demonstration


Copyright (c) 2017 The Hyve B.V. This notebook is licensed under the GNU General Public License, version 3. Authors: Ward Weistra.

We start by importing the tranSMART Python library (https://pypi.python.org/pypi/transmart) and connecting to the tranSMART server.


In [ ]:
import getpass
from transmart import TransmartApi

api = TransmartApi(
    host = 'http://transmart-test.thehyve.net',
    user = input('Username:'),
    password = getpass.getpass('Password:'),
    apiversion = 2)

api.access()

Next we import and configure Pandas, a Python library to work with data.


In [ ]:
import pandas as pd
from pandas.io.json import json_normalize

pd.set_option('max_colwidth', 1000)
pd.set_option("display.max_rows",100)
pd.set_option("display.max_columns",100)

Part 1: Plotting blood pressure over time

As a first REST API call it would be nice to see what studies are available in this tranSMART server.

You will see a list of all studies, their name (studyId) and what dimensions are available for this study. Remember that tranSMART previously only supported the dimensions patients, concepts and studies. Now you should see studies with many more dimensions!


In [ ]:
studies = api.get_studies()
json_normalize(studies['studies'])

We choose the TRAINING study and ask for all patients in this study. You will get a list with their patient details and patient identifier.


In [ ]:
study_id = 'TRAINING'
patients = api.get_patients(study = study_id)
json_normalize(patients['patients'])

Next we ask for the full list of observations for this study. This list will include one row per observation, with information from all their dimensions. The columns will have headers like <dimension name>.<field name> and numericValue or stringValue for the actual observation value.


In [ ]:
obs = api.get_observations(study = study_id)
obsDataframe = json_normalize(api.format_observations(obs))
obsDataframe

In [ ]:
#DO STUFF WITH THE TRAINING STUDY HERE

Part 2: Combining Glowing Bear and the Python client

For the second part we will work with the Glowing Bear user interface that was developed at The Hyve, funded by IMI Translocation and BBMRI.

An API is great to extract exactly the data you need and analyze that. But it is harder to get a nice overview of all data that is available and define the exact set to extract. That is where the Glowing Bear was built for.

Please go to http://glowingbear2-head.thehyve.net and create a Patient Set on the Data Selection tab (under Select patients). Once you have saved your patient set, copy the patient set identifier and paste that below.


In [ ]:
patient_set_id = 28733

Now let's return all patients for the patient set we made!


In [ ]:
patients = api.get_patients(patientSet = patient_set_id)
json_normalize(patients['patients'])

And do the same for all observations for this patient set.


In [ ]:
obs = api.get_observations(study = study_id, patientSet = patient_set_id)
obsDataframe = json_normalize(api.format_observations(obs))
obsDataframe

Now you know exactly how to retrieve data from a tranSMART 2017 server and analyze this with Python! Please feel free to change this code in anyway you like. And if you have any questions, reach us at our public forum via development@thehyve.nl or https://groups.google.com/a/thehyve.nl/forum/#!forum/development.